using System;
using System.Collections.Generic;
using Xunit;

namespace LanguageLearnTestCase.String
{
    /// <summary>
    /// 32位无符号整数 与 二进制字符串 互转
    /// </summary>
    public class UInt32ToBinaryString
    {
        public static IEnumerable<object[]> GetTestUInt32BindBinaryStringArgs()
        {
            return new object[][] {
                new object[] { 43261596, @"00000010100101000001111010011100" },
                new object[] { 964176192, @"00111001011110000010100101000000" },
                new object[] { 4294967293, @"11111111111111111111111111111101" },
                new object[] { 3221225471, @"10111111111111111111111111111111" },
            };
        }

        [Theory]
        [MemberData(nameof(GetTestUInt32BindBinaryStringArgs))]
        public void ToBinaryString(uint input, string output)
        {
            // 此步已经完成数值转二进制字符串
            string str = Convert.ToString(input, 2);
            // 将结果位数补充到指定需要的位数
            str = str.PadLeft(32, '0');
            Assert.Equal(output, str);
        }

        [Theory]
        [MemberData(nameof(GetTestUInt32BindBinaryStringArgs))]
        public void ToUInt32(uint output, string input)
        {
            uint value = Convert.ToUInt32(input, 2);
            Assert.Equal(output, value);
        }
    }
}
